<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2021/4/28
 * Time: 9:52
 */

namespace pay\plug\constructionBank;


use pay\libs\Curl;
use yii\base\Exception;

class CcbMain
{

    protected $MERCHANTID = "105000051938192";              // 商户代码
    protected $POSID = '054285328';                                       // 商户柜台代码
    protected $BRANCHID = "320000000";                      // 分行代码
    protected $PUB = "e955b034592d459c1aeccc83020111";
    // MAC=e52f2a424236642f278211745ea6aab5

    protected $url = "https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain?CCB_IBSVersion=V6";

    public $orderId;
    public $payMent;
    public $remark1;
    public $remark2;
    public $clientIp;
    public $tradeType;
    public $subAppid;
    public $subOpenid;


    public function getPayData()
    {
        $url = $this->getParams();
        $result = json_decode(Curl::get($url),true);
        if  ($result['SUCCESS'] = 'true'){
            $res = json_decode(Curl::get($result['PAYURL']),true);
            if ($res['SUCCESS'] == 'true' and $res['ERRCODE'] == '000000'){
                return $res;
            }else{
                throw new Exception($res['ERRMSG'],99);
            }
        }else{
            throw new Exception($result['ERRMSG'],99);
        }
    }
    

    public function getParams()
    {
        $params = [
            'MERCHANTID' => $this->MERCHANTID,
            'POSID' => $this->POSID,
            'BRANCHID' => $this->BRANCHID,
            'ORDERID' => $this->orderId,            // 订单号
            'PAYMENT' => $this->payMent,                                    // 付款金额
            'CURCODE' => '01',                                  // 币种
            'TXCODE' => '530590',                                    // 交易码
            'REMARK1' => $this->remark1,                                    // 备注1
            'REMARK2' => $this->remark2,                                    // 备注2
            'TYPE' => '1',                                  // 接口类型 1- 防钓鱼接口
            'PUB' => $this->PUB,
            'GATEWAY' => '0',                               // 默认送 0
            'CLIENTIP' => $this->clientIp,                               // 客户端 IP
            'REGINFO' => '',                                // 客户注册信息
            'PROINFO' => '',                                // 商品信息
            'REFERER' => '',                                        //商户 URL
//            'TIMEOUT' => '',                                        //订单超时时间
            'TRADE_TYPE' => empty($this->tradeType)?'MINIPRO':$this->tradeType,                                        // 交易类型 JSAPI-- 公 众 号 支 付 、MINIPRO--小程序
            'SUB_APPID' => $this->subAppid,                                        // 小程序/公众号的 APPID
            'SUB_OPENID' => $this->subOpenid,                                   //用户子标识 openid
        ];
        $mac = $this->getMac($params);
        $paramsData = $this->getParamsData($params);
        $paramsPost = $paramsData.'&MAC='.$mac;
        $url = $this->url.'&'.$paramsPost;
        return $url;
    }



    protected function getMac($paraMap,$urlEncode = false)
    {
        $buff = "";
        foreach ($paraMap as $k => $v) {
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar = '';
        if (strlen($buff) > 0) {
            $reqPar = substr($buff, 0, strlen($buff) - 1);
        }
        $sign = md5($reqPar);
        return $sign;
    }



    protected function getParamsData($paraMap)
    {
        $buff = "";
        unset($paraMap['PUB'],$paraMap['TIMEOUT']);
        foreach ($paraMap as $k => $v) {
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar = '';
        if (strlen($buff) > 0) {
            $reqPar = substr($buff, 0, strlen($buff) - 1);
        }
        return $reqPar;
    }


}